﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace PizzaLunch
{
    public class UsersList : ObservableCollection<UserInfo>
    {
    }

    public class UserInfo : INotifyPropertyChanged
    {
        public class DynaInfo
        {
            private long _lastComposeStamp;
            public virtual long LastComposeStamp
            {
                get { return _lastComposeStamp; }
                set { _lastComposeStamp = value; }
            }

            private string _lastComposeID;
            public virtual string LastComposeID
            {
                get { return _lastComposeID; }
                set { _lastComposeID = value; }
            }

            public virtual bool FriendsCompleted { set; get; }
            public virtual bool FansCompleted { set; get; }

            public DynaInfo()
            {
                _lastComposeID = "0";
                _lastComposeStamp = 0;
                FriendsCompleted = false;
                FansCompleted = false;
            }
        }

        private DynaInfo _dyna;
        /*public virtual DynaInfo Dyna
        {
            get { return _dyna; }
        }*/
        public virtual DynaInfo this[TweetAccount tac]
        {
            get { return _dyna; }
        }

        public virtual bool Updated { get; set; }

        private string _id;
        public virtual string ID { set { _id = value; } }
        public virtual string GetID(TweetAccount tac)
        {
            return _id;
        }

        public virtual string ShowName { get; set; }
        public virtual bool VIP { get; set; }
        public virtual string HeadUrl { get; set; }
        public virtual string Location { get; set; }
        public virtual string Gender { get; set; }
        public virtual DateTime Birthday { get; set; }
        public virtual string Signature { get; set; }
        public virtual long friends { get; set; }
        public virtual long follows { get; set; }
        public virtual long topics { get; set; }

        public UsersList FriendsList { get; set; }
        public UsersList FansList { get; set; }

        public TweetList ItemList = new TweetList();

        private TweetAccount _account;
        public virtual TweetAccount Account
        {
            get { return _account; }
        }

        public enum UserType
        {
            USER_SELF,
            USER_FRIENDS,
            USER_FANS,
            USER_OTHERS,
        }

        public UserInfo(TweetAccount acs)
        {
            _account = acs;
            FriendsList = new UsersList();
            FansList = new UsersList();
            _dyna = new DynaInfo();
            Updated = false;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion

        public virtual void GetUserInfo()
        {
            Account.GetUserInfoByID(this);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;

            UserInfo user = obj as UserInfo;
            if ((System.Object)user == null)
                return false;

            if (user.GetType() != GetType())
                return false;

            if (Account.Equals(user.Account) && GetID(Account) == user.GetID(user.Account))
                return true;
            return false;
        }
    }

    public class CurrentUser : UserInfo
    {
        public CurrentUser(TweetAccount acs):base(acs)
        {
        }

        public override bool Updated
        {
            get { return true; }
            set { return; }
        }

        public override string GetID(TweetAccount tac)
        {
            return tac.ID;
        }

        /*public override string ID
        {
            get
            {
                return _defaultAccount.AccountInfo.ID;
            }
        }*/

        public override string ShowName
        {
            get
            {
                return _defaultAccount.AccountInfo.ShowName;
            }
        }

        public override bool VIP
        {
            get
            {
                return _defaultAccount.AccountInfo.VIP;
            }
        }

        public override string HeadUrl
        {
            get
            {
                return _defaultAccount.AccountInfo.HeadUrl;
            }
        }

        public override string Location
        {
            get
            {
                return _defaultAccount.AccountInfo.Location;
            }
        }

        public override string Gender
        {
            get
            {
                return _defaultAccount.AccountInfo.Gender;
            }
        }

        public override DateTime Birthday
        {
            get
            {
                return _defaultAccount.AccountInfo.Birthday;
            }
        }

        public override string Signature
        {
            get
            {
                return _defaultAccount.AccountInfo.Signature;
            }
        }

        public override long friends
        {
            get
            {
                return MainControl.AllAcount.Sum( tac=>tac.AccountInfo.friends );
            }
        }

        public override long follows
        {
            get
            {
                return MainControl.AllAcount.Sum(tac => tac.AccountInfo.follows);
            }
        }

        public override long topics
        {
            get
            {
                return MainControl.AllAcount.Sum(tac => tac.AccountInfo.topics);
            }
        }

        private TweetAccount _defaultAccount
        {
            get
            {
                TweetAccount tac = MainControl.GetDefaultAccount();
                if (tac != null)
                    return tac;
                tac = MainControl.GetDefaultAccount();
                if (tac != null)
                    return tac;
                if (MainControl.AllAcount.Count > 0)
                    return MainControl.AllAcount[0];
                return new TweetAccount(null);
            }
        }

        public override DynaInfo this[TweetAccount tac]
        {
            get { return tac.Dyna; }
        }

        public override TweetAccount Account
        {
            get { return _defaultAccount; }
        }

        public override void GetUserInfo()
        {
            return;
        }

        /*public TweetAccount _currentAccount;
        public TweetAccount CurrentAccount
        {
            get
            {
                if (_currentAccount != null)
                    return _currentAccount;
                else
                {
                    TweetAccount tac = MainControl.GetDefaultAccount();
                    if (tac!=null)
                        return tac;
                    if (MainControl.AllAcount.Count>0)
                        return MainControl.AllAcount[0];
                    return new TweetAccount(null);
                }
            }
            set { _currentAccount = value; }
        }

        /*public override long LastComposeStamp
        {
            get { return Account._lastComposeStamp; }
            set { Account._lastComposeStamp = value; }
        }

        public override string LastComposeID
        {
            get { return Account._lastComposeID; }
            set { Account._lastComposeID = value; }
        }

        public override DynaInfo Dyna
        {
            get { return Account.Dyna; }
        }*/

    }
}
